<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>招聘信息管理管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加招聘信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 提示区域 -->
      <el-alert
        title="添加招聘信息"
        type="info"
        center
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 步骤条区域 -->
      <el-steps
        :space="200"
        :active="activeIndex - 0"
        finish-status="success"
        align-center
      >
        <el-step title="企业信息"></el-step>
        <el-step title="招聘信息"></el-step>
        <el-step title="岗位信息"></el-step>
        <el-step title="岗位要求"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- tab栏区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
        label-position="top"
      >
        <el-tabs
          v-model="activeIndex"
          :tab-position="'left'"
          :before-leave="beforeTabLeave"
          @tab-click="tabClicked"
        >
          <el-tab-pane label="企业信息" name="0">
            <el-form-item label="企业编号" prop="companyid">
              <el-input v-model="company.companyid"  :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="企业名称" prop="companyname">
              <el-input v-model="company.companyname"  :disabled="true" ></el-input>
            </el-form-item>
            <el-form-item label="负责人" prop="principal">
              <el-input v-model="company.principal"  :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="company.phone" :disabled="true"></el-input>
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="招聘者要求" name="1">
            <el-form-item label="学历" prop="eduname">
              <el-select v-model="addForm.eduid" placeholder="请选择">
                <el-option
                  v-for="item in edulist"
                  :key="item.eduname"
                  :label="item.eduname"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="岗位信息" name="2">
            <el-form-item label="招聘岗位" prop="jobs">
              <el-input v-model="addForm.jobs"></el-input>
            </el-form-item>
            <el-form-item label="招聘人数" prop="numbers">
              <el-input v-model="addForm.numbers" type="number"></el-input>
            </el-form-item>
            <el-form-item label="薪资" prop="salary">
              <el-input v-model="addForm.salary" ></el-input>
            </el-form-item>
            <el-form-item label="岗位类别" prop="catename">
              <el-cascader
                expand-trigger="hover"
                :options="catelist"
                :props="cateProps"
                v-model="addForm.cateids"
              >
              </el-cascader>
            </el-form-item>
            <el-form-item label="工作地点" prop="placename">
              <el-cascader
              expand-trigger="hover"
              :options="workplace"
              :props="workplaceProps"
              v-model="addForm.placeids"
            >
            </el-cascader>
            <el-input v-if="addForm.placeids!=null" v-model="addForm.place"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="岗位要求" name="3">
            <!-- 富文本编辑器组件 -->
            <quill-editor v-model="addForm.jobrequirements"></quill-editor>
            <!-- 添加商品的按钮 -->
            <el-button type="primary" class="btnAdd" @click="add"
              >添加商品</el-button
            >
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import _ from 'lodash'

export default {
  data() {
    // 验证手机号的规则
    var checkphone = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regphone = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
      if (regphone.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的手机号'))
    }
    return {
      activeIndex: '0',
      // 添加商品的表单数据对象
      addForm: {
        companyid: '',
        companyname: '',
        principal: '',
        phone: '',
        eduid: '',
        jobs: '',
        numbers: '',
        cateids: '',
        jobrequirements: '',
      },
      //添加表单的验证规则对象
      addFormRules: {
        jobs: [
          { required: true, message: '请输入招聘岗位', trigger: 'blur' },
          {
            min: 2,
            max: 100,
            message: '招聘岗位的长度在2~30之间',
            trigger: 'blur',
          },
        ],
        numbers: [
          { required: true, message: '请输入招聘人数', trigger: 'blur' },
        ],
        cateid: [
          { required: true, message: '请输入岗位类别', trigger: 'blur' },
        ],
        eduid: [{ required: true, message: '请输入学历', trigger: 'blur' }],
        jobrequirements: [
          { required: true, message: '请输入岗位要求', trigger: 'blur' },
        ],
      },
      // 岗位分类列表
      catelist: [],
      cateProps: {
        label: 'catename',
        value: 'id',
        children: 'children',
      },
      edulist: [],
      eduProps: {
        label: 'eduname',
        value: 'id',
      },
      workplace: [],
      workplaceProps: {
        label: 'placename',
        value: 'id',
        children: 'children',
      },
      company: {},
    }
  },
  created() {
    this.getCateList(), 
    this.educationlist(),
    this.getCompanyById(),
    this.getWorkplaceList()

  },
  methods: {
    // 获取所有岗位分类数据
    async getCateList() {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error('获取岗位分类数据失败')
      }
      this.catelist = res.data
      console.log(this.catelist);
    },
    // 获取所有学历分类数据
    async educationlist() {
      const { data: res } = await this.$http.get('educations')
      if (res.meta.status !== 200) {
        return this.$message.error('获取学历数据失败')
      }
      this.edulist = res.data
      console.log(this.edulist)
    },
    // 获取所有工作地点分类数据
    async getWorkplaceList() {
      const { data: res } = await this.$http.get('workplace')

      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.workplace = res.data
    },
    // 级联选择器选中项变化，会触发这个函数
  
    beforeTabLeave(activeName, oldActiveName) {},
    async tabClicked() {},
    //查找企业
    async getCompanyById() {
      this.user = JSON.parse(window.sessionStorage.getItem('user'))
      const {data:res} =await this.$http.get('company/' + this.user.actorid)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.company = res.data
    },

    // 添加商品
    add() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) {
          return
        }
        this.addForm.jobrequirements = this.addForm.jobrequirements.slice(3, -4)
        const form = _.cloneDeep(this.addForm)
        console.log(form);
        form.placeids = form.placeids.join(',')
        console.log(1);
        form.cateids = form.cateids.join(',')
        console.log(form);

        const { data: res } = await this.$http.post('AddRecruit/'+this.user.actorid, form)
        if (res.meta.status != 201) {
          this.$message.error(res.meta.msg)
        }
        this.$message.success(res.meta.msg)
        //隐藏
        this.$router.push('/ComRecruit')
      })
    },
  },
}
</script>

<style lang="less" scoped>
.el-checkbox {
  margin: 0 10px 0 0 !important;
}

.previewImg {
  width: 100%;
}

.btnAdd {
  margin-top: 15px;
}
</style>
